<template>
  <a-modal :width="1200" v-model:visible="showDetail" :footer="false" title="订单详情" title-align="start" :mask-closable="false" @cancel="closedDialog">
    <div class="purchase-order-detail">
      <div class="header">
        <p class="title">基础信息</p>
      </div>
      <a-row class="detail-item">
        <a-col :span="8">
          <div class="flex">
            <p class="label">创建人：</p>
            <p class="val">{{ detailInfo.createUname }}</p>
          </div>
        </a-col>
        <a-col :span="8">
          <div class="flex">
            <p class="label">创建时间：</p>
            <p class="val">{{ $setTime(detailInfo.gmtCreate) }}</p>
          </div>
        </a-col>
      </a-row>
      <a-row class="detail-item">
        <a-col :span="8">
          <div class="flex">
            <p class="label">交货单号：</p>
            <div class="flex-c">
              <span class="highlight">{{ detailInfo.billNo }}</span>
              <CopyText style="margin-left: 5px" :val="detailInfo.billNo" />
            </div>
          </div>
        </a-col>
        <a-col :span="8" v-if="source == 'purchase'">
          <div class="flex">
            <p class="label">供应商：</p>
            <p class="val">{{ detailInfo.supplierName }}（{{ detailInfo.supplierCode }}）</p>
          </div>
        </a-col>
        <a-col :span="8" v-if="source == 'supplier'">
          <div class="flex">
            <p class="label">采购方：</p>
            <p class="val">{{ detailInfo.companyName }}</p>
          </div>
        </a-col>
        <a-col :span="8">
          <div class="flex">
            <p class="label">收货仓库：</p>
            <p class="val">{{ detailInfo.warehouseName }}（{{ detailInfo.warehouseCode }}）</p>
          </div>
        </a-col>
      </a-row>
      <a-row class="detail-item" v-if="source == 'supplier'">
        <a-col :span="8">
          <div class="flex">
            <p class="label">供应商：</p>
            <p class="val">{{ detailInfo.supplierName }}（{{ detailInfo.supplierCode }}）</p>
          </div>
        </a-col>

        <a-col :span="8">
          <div class="flex">
            <p class="label">本地交货仓库：</p>
            <p class="val" v-if="detailInfo.outWarehouseName">{{ detailInfo.outWarehouseName }}（{{ detailInfo.outWarehouseCode }}）</p>
          </div>
        </a-col>
      </a-row>
      <div class="purchase-detail-order">
        <a-tabs style="margin-bottom: 10px" v-model:active-key="activeTab">
          <a-tab-pane key="1" title="交货单明细" />
          <a-tab-pane key="2" title="操作日志" />
        </a-tabs>
        <div style="flex: 1; overflow: hidden">
          <a-table v-show="activeTab == '1'" class="table-two-line show-border-table" ref="tableRef" :data="detailInfo.billItems" :scroll="{ x: '100%', y: '100%' }" size="mini" :pagination="false">
            <template #columns>
              <a-table-column v-for="(item, index) of tableColumns" :key="index" :fixed="item.fixed" :align="item.align" :width="item.width" :title="item.title" :data-index="item.dataIndex">
                <template v-if="item.dataIndex == 'purchaseNo'" #cell="{ record }">
                  <p class="highlight"> {{ record.purchaseNo }}</p>
                </template>
                <template v-else-if="item.dataIndex == 'img'" #cell="{ record }">
                  <ImgPopover :src="record.productImg">
                    <template #con>
                      <a-image width="40" height="40" fit="contain" :src="record.productImg" />
                    </template>
                  </ImgPopover>
                </template>
                <template v-else-if="item.dataIndex == 'sku'" #cell="{ record }">
                  <p class="highlight cursor">{{ record.sku }}</p>
                </template>
                <template v-else-if="item.dataIndex == 'productSpecs'" #cell="{ record }">
                  <TextLimit :val="getProductSpecs(record)" />
                </template>
                <template v-else-if="item.dataIndex == 'price'" #cell="{ record }">
                  <p>材质：￥{{ record.materialPrice }}</p>
                  <p>产品：￥{{ record.productPrice }}</p>
                  <p>工费：￥{{ record.laborPrice }}</p>
                </template>
                <template v-else-if="item.dataIndex == 'rate'" #cell="{ record }">
                  <p>{{ record.rate }}%</p>
                  <p class="highlight">{{ record.priceType == 1 ? '含税' : '不含税' }}</p>
                </template>
                <template v-else-if="['otherAmount', 'materialAmount', 'productAmount', 'laborAmount', 'totalAmount', 'taxAmount', 'singleAmount'].includes(item.dataIndex)" #cell="{ record }">
                  <p>￥{{ record[item.dataIndex] }}</p>
                </template>
              </a-table-column>
            </template>
          </a-table>
          <PurchaseLogs v-if="activeTab == '2'" :currentData="detailInfo" api="findDeliveryLogPageV2" />
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import PurchaseLogs from '@/views/purchaseApplication/components/purchaseLogs.vue'
  import { getDeliveryInfo } from '@/api/purchaseApplication'
  import { parseJson } from '@/utils'
  const emit = defineEmits(['closed'])
  const props = defineProps({
    isShow: {
      type: Boolean,
      default: false,
    },
    source: {
      type: String,
      default: 'purchase',
    },
    currentId: {
      type: String,
    },
  })
  const tableColumns: Array<any> = [
    {
      title: '采购单',
      dataIndex: 'purchaseNo',
      width: 120,
    },
    {
      title: '图片',
      dataIndex: 'img',
      width: 60,
    },
    {
      title: 'SKU',
      dataIndex: 'sku',
      width: 100,
    },
    {
      title: 'SKU规格',
      dataIndex: 'productSpecs',
      width: 100,
    },
    {
      title: '采购员',
      dataIndex: 'purchaseUname',
      width: 70,
    },
    ...(props.source == 'purchase'
      ? [
          {
            title: '需求人',
            dataIndex: 'salesmanName',
            width: 70,
          },
        ]
      : []),
    {
      title: '单价',
      dataIndex: 'price',
      width: 100,
    },

    {
      title: '税率',
      dataIndex: 'rate',
      width: 60,
    },
    {
      title: '交货数量',
      dataIndex: 'deliveryAmt',
      width: 70,
    },

    {
      title: '交货材质重量(g)',
      dataIndex: 'materialWeight',
      width: 110,
    },
    {
      title: '实际签收数量',
      dataIndex: 'deliveryRealAmt',
      width: 90,
    },
    {
      title: '实收材质重量(g)',
      dataIndex: 'materialRealWeight',
      tip: '实收材质重量=(交货材质重量/交货数量)*实际签收数量',
      width: 120,
    },
    {
      title: '其他费用',
      dataIndex: 'otherAmount',
      tip: '本次交货产生其他费用',
      width: 85,
    },
    {
      title: '材质金额',
      dataIndex: 'materialAmount',
      tip: '材质金额：--签收前=材质单价*交货材质重量--签收后=材质单价*实收材质重量',
      width: 85,
    },
    {
      title: '产品金额',
      dataIndex: 'productAmount',
      tip: '产品金额：--签收前=产品单价*交货数量--签收后=产品单价*实际签收数量',
      width: 85,
    },
    {
      title: '工费',
      dataIndex: 'laborAmount',
      tip: '工费金额：--签收前=工费单价*交货数量--签收后=工费单价*实际签收数量',
      width: 85,
    },
    {
      title: '合计金额',
      dataIndex: 'totalAmount',
      tip: '合计金额=材质金额+产品金额+工费+其他费用',
      width: 85,
    },
    {
      title: '税金',
      dataIndex: 'taxAmount',
      tip: '税金=税率*合计金额',
      width: 85,
    },
    {
      title: '单件货值',
      dataIndex: 'singleAmount',
      tip: '单件货值(含税)=（合计金额-税金）/实际签收数量--单件货值(不含税)=合计金额/实际签收数量',
      width: 85,
    },
  ]

  const showDetail = ref(props.isShow)
  const detailInfo = ref<any>({})
  const activeTab = ref('1')

  // 关闭
  function closedDialog() {
    showDetail.value = false
    emit('closed')
  }

  // 获取详情
  async function getDetail() {
    if (!props.currentId) return
    const res: any = await getDeliveryInfo({ id: props.currentId })
    detailInfo.value = res?.data || {}
    detailInfo.value.billItems.forEach((item: any) => {
      let purchaseSpecs = item.purchaseSpecs ? parseJson(item.purchaseSpecs, {}) : {}
      item.purchaseSpecsArr = Object.values(purchaseSpecs)
    })
  }
  getDetail()
  // 显示sku规格
  function getProductSpecs(item: any) {
    let productSpecs = item.productSpecs ? parseJson(item.productSpecs, []) : []
    return productSpecs.map((el: any) => el.valueCn)?.join('*') || '--'
  }
</script>

<style lang="less" scoped>
  .purchase-order-detail {
    height: calc(84vh - 100px);
    display: flex;
    flex-direction: column;
    .header {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 10px;
      border-left: 3px solid rgba(var(--primary-6), 0.5);
      padding-left: 10px;
      .title {
        margin-right: 30px;
        width: 90px;
      }
    }
    .purchase-detail-order {
      margin-top: 20px;
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .arco-tab {
        margin-top: 10px;
      }
    }
    .detail-item {
      margin-bottom: 5px;
      .label {
        width: 120px;
        text-align: right;
      }
      .val {
        color: #666;
      }
      .info {
        font-size: 12px;
        color: #999;
      }
    }
  }
</style>
